package thread;

class Counter {
    public static int count = 0;

    /*synchronized  void counteradd() {
        count++;
    }*/

    //这两种方法等效，并且synchronized修饰方法就是因为this,使用this作为锁对象
    void counteradd() {
        synchronized (this) {
            count++;
        }
    }
}

public class Demo19 {

    private static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (counter) {
                    counter.counteradd();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.counteradd();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count:" + counter.count);
    }
}
